<template>
  <div class="app-container">
    <div class="table-layout">
      <el-row>
        <el-col :span="4" class="table-cell-title">名稱</el-col>
        <el-col :span="4" class="table-cell-title">总发行量</el-col>
        <el-col :span="4" class="table-cell-title">使用類型</el-col>
        <el-col :span="4" class="table-cell-title">使用門檻</el-col>
        <el-col :span="4" class="table-cell-title">面額</el-col>
        <el-col :span="4" class="table-cell-title">狀態</el-col>
      </el-row>
      <el-row>
        <el-col :span="4" class="table-cell">{{ couponMessage.nameCn }}</el-col>
        <el-col :span="4" class="table-cell">{{ couponMessage.publishCount }}</el-col>
        <el-col :span="4" class="table-cell">{{ dict.label.useType[couponMessage.useType] }}</el-col>
        <el-col :span="4" class="table-cell">满{{ couponMessage.minPoint }}元可用</el-col>
        <el-col :span="4" class="table-cell">{{ couponMessage.amount }}元</el-col>
        <el-col :span="4" class="table-cell">{{ dict.label.couponStatus[couponMessage.couponStatus] }}</el-col>
      </el-row>
      <el-row>
        <el-col :span="4" class="table-cell-title">開始時間</el-col>
        <el-col :span="4" class="table-cell-title">結束時間</el-col>
        <el-col :span="4" class="table-cell-title">已领取</el-col>
        <el-col :span="4" class="table-cell-title">待领取</el-col>
        <el-col :span="4" class="table-cell-title">已使用</el-col>
        <el-col :span="4" class="table-cell-title">未使用</el-col>
      </el-row>
      <el-row>
        <el-col :span="4" class="table-cell" style="font-size: 13px">
          <div>{{ couponMessage.startTime }}</div>
        </el-col>
        <el-col :span="4" class="table-cell" style="font-size: 13px">
          <div>{{ couponMessage.endTime }}</div>
        </el-col>
        <el-col :span="4" class="table-cell">{{ couponMessage.receiveCount }}</el-col>
        <el-col :span="4" class="table-cell">{{ couponMessage.publishCount-couponMessage.receiveCount }}</el-col>
        <el-col :span="4" class="table-cell">{{ couponMessage.useCount }}</el-col>
        <el-col :span="4" class="table-cell">{{ couponMessage.publishCount-couponMessage.useCount }}</el-col>
      </el-row>
    </div>
    <el-card class="filter-container" shadow="never">
      <div>
        <i class="el-icon-search" />
        <span>筛选搜索</span>
        <el-button
          style="float:right"
          type="primary"
          size="small"
          @click="handleSearchList()"
        >
          查询搜索
        </el-button>
        <el-button
          style="float:right;margin-right: 15px"
          size="small"
          @click="handleResetSearch()"
        >
          重置
        </el-button>
      </div>
      <div style="margin-top: 15px">
        <el-form :inline="true" :model="listQuery" label-width="140px">
          <el-form-item label="使用狀態：">
            <el-select v-model="listQuery.useStatus" placeholder="使用狀態" clearable class="input-width">
              <el-option
                v-for="item in dict.useStatus"
                :key="item.id"
                :label="item.dictValue"
                :value="item.dictKey"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="订单编号：">
            <el-input v-model="listQuery.orderSn" class="input-width" placeholder="订单编号" />
          </el-form-item>
        </el-form>
      </div>
    </el-card>
    <div class="table-container">
      <el-table
        ref="couponHistoryTable"
        v-loading="listLoading"
        :data="list"
        style="width: 100%;"
        border
        stripe
      >
        <el-table-column label="领取会员" width="140"      >
          <template v-if="scope.row.member" slot-scope="scope">{{ scope.row.member.username }}</template>
        </el-table-column>
        <el-table-column label="领取方式" width="100"      >
          <template slot-scope="scope">{{ dict.label.getType[scope.row.getType] }}</template>
        </el-table-column>
        <el-table-column label="使用方式" width="100"      >
          <template slot-scope="scope">{{ dict.label.useStatus[scope.row.useStatus] }}</template>
        </el-table-column>
        <el-table-column label="领取时间" width="160"      >
          <template slot-scope="scope">{{ scope.row.createdAt }}</template>
        </el-table-column>
        <el-table-column label="使用时间" width="160"      >
          <template slot-scope="scope">{{ scope.row.useTime }}</template>
        </el-table-column>
        <el-table-column label="订单编号"      >
          <template slot-scope="scope">{{ scope.row.orderSn }}</template>
        </el-table-column>
      </el-table>
    </div>
    <div class="pagination-container">
      <el-pagination
        background
        layout="total, sizes,prev, pager, next,jumper"
        :current-page.sync="listQuery.page"
        :page-size="listQuery.size"
        :page-sizes="[5,10,15]"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
  </div>
</template>

<script>
import { getOne, couponHistory } from '@/api/sms/coupon'
import { setTitle } from '@/utils'

const defaultListQuery = {
  page: 1,
  size: 10,
  couponStatus: null,
  orderSn: null,
  couponId: null
}
export default {
  name: 'History',
  dicts: ['couponType', 'useType', 'couponStatus', 'useStatus', 'getType'],
  data() {
    return {
      couponMessage: {},
      couponUserMessage: [],
      listQuery: Object.assign({}, defaultListQuery),
      listLoading: false,
      total: null,
      list: []
    }
  },
  created() {
    this.getMessage()
    this.listQuery.couponId = this.$route.query.id
    this.getList()
  },
  methods: {
    getMessage() {
      getOne(this.$route.query.id).then(res => {
        if (res.code === 0) {
          this.couponMessage = res.data
          setTitle('優惠券名稱' + this.couponMessage.nameCn)
        }
      })
    },
    getList() {
      this.listLoading = true
      couponHistory(this.listQuery).then(response => {
        this.listLoading = false
        this.list = response.data.rows
        this.total = response.data.total
      })
    },
    handleResetSearch() {
      this.listQuery = Object.assign({})
      this.listQuery.couponId = this.$route.query.id
    },
    handleSearchList() {
      this.listQuery.page = 1
      this.getList()
    },
    handleSizeChange(val) {
      this.listQuery.page = 1
      this.listQuery.size = val
      this.getList()
    },
    handleCurrentChange(val) {
      this.listQuery.page = val
      this.getList()
    }
  }
}

</script>

<style scoped>
.app-container {
  width: 80%;
  margin: 20px auto;
}
.filter-container {
  margin-top: 20px;
}
.table-layout {
  margin-top: 20px;
  border-left: 1px solid #DCDFE6;
  border-top: 1px solid #DCDFE6;
}
.table-cell {
  height: 60px;
  line-height: 40px;
  border-right: 1px solid #DCDFE6;
  border-bottom: 1px solid #DCDFE6;
  padding: 10px;
  font-size: 14px;
  color: #606266;
  text-align: center;
  overflow: hidden;
}
.table-cell-title {
  border-right: 1px solid #DCDFE6;
  border-bottom: 1px solid #DCDFE6;
  padding: 10px;
  background: #F2F6FC;
  text-align: center;
  font-size: 14px;
  color: #303133;
}
</style>
